<?php

class Default_Model_DbTable_User extends Zend_Db_Table_Abstract
{

    protected $_name = 'users';


    public function getAllUser(){
    	return $this->fetchAll();
    }
    
    public function getUserByLevel($level){
    	$select = $this->select()
    	->from($this->_name)
    	->where("level = $level");
    	return $this->fetchAll($select);
    }
    
    public function getUserByID($uId){
    	$select = $this->select()
    	->from($this->_name)
    	->where("user_id = $uId");
    	return $this->fetchAll($select);
    }
    
    public function addUser($uName,$pwd,$level,$fName,$lName,$addr,$phone,$saleName,$aff,$email,$d_create,$stt){
    	$userData = array(
    			"user_name"=>$uName,
    			"password"=>$pwd,
    			"level"=>$level,
    			"f_name"=>$fName,
    			"l_name"=>$lName,
    			"address"=>$addr,
    			"phone"=>$phone,
    			"sale_name"=>$saleName,
    			"aff"=>$aff,
    			"email"=>$email,
    			"date_create"=>$d_create,
    			"status"=>$stt
    			);
    	$this->insert($userData);
    	return $this->_db->lastInsertId("users");
    }
    
    public function addSeller($uName,$pwd,$level,$fName,$lName,$addr,$phone,$saleName,$aff,$email,$d_create,$stt,$title,$info,$logo){
    	$userData = array(
    			"user_name"=>$uName,
    			"password"=>$pwd,
    			"level"=>$level,
    			"f_name"=>$fName,
    			"l_name"=>$lName,
    			"address"=>$addr,
    			"phone"=>$phone,
    			"sale_name"=>$saleName,
    			"aff"=>$aff,
    			"email"=>$email,
    			"date_create"=>$d_create,
    			"status"=>$stt
    			);
    	$this->insert($userData);
    	$user_id = $this->_db->lastInsertId("users");
    	$msellerInfo = new Default_Model_DbTable_SellerInfo();
    	return $msellerInfo->addNewSaler($user_id, $title, $info, $logo);
    }
    
    public function updateUser($uId,$uName,$pwd,$level,$fName,$lName,$addr,$phone,$saleName,$aff,$email,$d_create,$stt){
    	$userData = array(
    			"user_name"=>$uName,
    			"password"=>$pwd,
    			"level"=>$level,
    			"f_name"=>$fName,
    			"l_name"=>$lName,
    			"address"=>$addr,
    			"phone"=>$phone,
    			"sale_name"=>$saleName,
    			"aff"=>$aff,
    			"email"=>$email,
    			"date_create"=>$d_create,
    			"status"=>$stt
    	);
    	return $this->update($userData, "user_id = $uId");
    }
    
    public function delUser($uId){
    	return $this->delete("user_id = $uId");
    }
    
    public function CheckUser($username){
    	return $this->fetchRow("user_name = $username");
    }
    
    public function checkEmail($email){
    	return $this->fetchRow("email = $email");
    }
}

